<template>
  <div>
    <h1>优惠券</h1>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型查询">
              <el-select v-model="form.cname">
                  <el-option v-for="item in couponsTypes" :key="item.canme" :label="item.canme" :value="item.canme"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间-结束时间">
              <el-date-picker
                  v-model="createTimes"
                  type="datetimerange"
                  range-separator="To"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  format="YYYY-MM-DD HH:mm:ss"
                  value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
          <!--添加按钮-->
          <div class="tools-div">
            <el-button size="small" type="success" @click="addCouponsType">添加卡券类型</el-button>
            <el-button size="small" type="success" @click="addCoupons">添加卡券</el-button>
            <el-button size="small" type="success" @click="overdueUser">禁用</el-button>
            <el-button size="small" type="success" @click="enableUser">列表</el-button>
          </div>
        </el-row>
      </el-form>
    </div>
    <!---数据表格-->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="title" label="名称" />
      <el-table-column prop="canme" label="类型" />
      <el-table-column prop="createtime" label="开始时间" />
      <el-table-column prop="timelimits" label="结束时间" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="danger" size="small" @click="updateById(scope.row)">
          {{ scope.row.state == 0 ? '禁用' : '启用' }}
        </el-button>
        <el-button type="danger" v-show="scope.row.state===1" size="small" @click="deleteById(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>

    <!--分页条-->
    <el-pagination
        v-model:current-page="pageParamsList.page"
        v-model:page-size="pageParamsList.limit"
        :page-sizes="[2, 20, 50, 100]"
        @size-change="fetchSysUser"
        @current-change="fetchSysUser"
        layout="total, sizes, prev, pager, next"
        :total="total"
    />
    <el-dialog v-model="dialogVisible" title="添加卡券类型" width="30%">
      <el-form label-width="120px" :model="addTypesForm">
        <el-form-item label="卡券类型">
            <el-input v-model="addTypesForm.canme" placeholder="请输入卡券类型"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveTypes">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog v-model="dialogVisibleCoupons" title="添加卡券" width="30%">
      <el-form label-width="120px" :model="addCouponsFormRef">
        <el-form-item label="卡券名称">
          <el-input v-model="addCouponsFormRef.title" placeholder="请输入卡券名称"/>
        </el-form-item>
        <el-form-item label="卡券类型">
            <el-select v-model="addCouponsFormRef.ctypes">
                <el-option v-for="item in couponsTypes" :key="item.id" :label="item.canme" :value="item.id"/>
            </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
              v-model="addCouponsFormRef.createtime"
              type="datetime"
              placeholder="Pick a Date"
              format="YYYY-MM-DD HH:mm:ss"
              date-format="MMM DD, YYYY"
              time-format="HH:mm"
              :disabled-date="disabledDate"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
              v-model="addCouponsFormRef.timelimits"
              type="datetime"
              placeholder="Pick a Date"
              format="YYYY-MM-DD HH:mm:ss"
              date-format="MMM DD, YYYY"
              time-format="HH:mm"
              :disabled-date="disabledDate"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveCoupons">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {
  addCouponsTypes, getaddCoupons,
  getCouponsList,
  getCouponsTypes,
  getCouponsUpdateState,
  getDeleteCoupons
} from "@/api/changebattery/coupons";

const disabledDate = (time) => {
  return time.getTime() <= Date.now()
}
const addTyeps={
  canme:''
}
const addTypesForm= ref(addTyeps)

const dialogVisible =ref(false)

const saveTypes =  async () =>{
  const { code } = await addCouponsTypes(addTypesForm.value)
  if (code === 200) {
    ElMessage.success('添加成功')
    dialogVisible.value=false
    await fetchSysUser()
  }
}


//添加卡券类型
const addCouponsType = () =>{
dialogVisible.value=true
}

const dialogVisibleCoupons =ref(false)

const addCouponsForm={
  title:'',
  ctypes:'',
  createtime:'',
  timelimits:'',
}
const addCouponsFormRef=ref(addCouponsForm)


//添加卡券
const addCoupons = () =>{
  dialogVisibleCoupons.value=true
}

const saveCoupons = async ()=>{
  const {code} = await getaddCoupons(addCouponsFormRef.value)
  if (code === 200) {
    ElMessage.success('添加成功')
    dialogVisibleCoupons.value=false
    await fetchSysUser()
  }
}


//开始和结束时间数据模型
const createTimes = ref([])

const couponsTypes= ref([])
const list = ref([])

const queryDto = {
  createTimeBegin: '',
  createTimeEnd: '',
  state:0,
  cname:'',
}

const form = ref(queryDto)

//重置
const resetData = async () => {
  form.value.cname = ''
  form.value.createTimeBegin = ''
  form.value.createTimeEnd = ''
  createTimes.value=[]
  await fetchSysUser()
}
// 删除数据
const deleteById = row => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getDeleteCoupons(row.id)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParamsUsers.value.page = 1
      await fetchSysUser()
    }
  })
}

//否用用户
const overdueUser = () =>{
  form.value.state=1
  fetchSysUser()
}

//启用用户
const enableUser = () =>{
  form.value.state=0
  fetchSysUser()
}



//上架下架
const updateById = async (row) => {
  const { code } = await getCouponsUpdateState(row.id)
  ElMessage.success('操作成功')
  await fetchSysUser(pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value)
}

//搜索方法
const searchSysUser = () => {
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}

//列表类型方法
const fetchCouponsTypes = async () => {
  const { data } = await getCouponsTypes()
  couponsTypes.value = data
}



//列表
const fetchSysUser = async () => {
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getCouponsList(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
  list.value = data.coupons.records
  total.value = data.coupons.total
}
onMounted(async () => {
  await fetchSysUser()
  await fetchCouponsTypes()
})
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)

</script>

<style scoped>

</style>
